package com.saret.utils;

import org.codebridge.translator.Language;

import java.io.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * User: biniam.gebremichael
 * Date: 2/3/11
 */
public class UtfFileHandle {

    public static BufferedReader utfIn(File fileName) throws IOException {
        if (!fileName.exists()) {
            fileName.getParentFile().mkdirs();
            fileName.createNewFile();
        }
        return new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8"));
    }

    public static BufferedWriter utfOut(File fileName) throws FileNotFoundException, UnsupportedEncodingException {
        return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8"));
    }

    public static void readFile(File wlFile, Consumer<String> consumer) throws IOException {
        if(wlFile==null||!wlFile.exists()){
            System.out.println(wlFile + " does not exist");
        }
        BufferedReader utfIn = UtfFileHandle.utfIn(wlFile);
        String aLine;
        try {
            while ((aLine = utfIn.readLine()) != null) consumer.consume(aLine);
        } finally {
            if (utfIn != null) utfIn.close();
        }
    }

    public static Map<String, List<String>> readDictionary(File file, final String separator1, final String separator2) {
        final ConcurrentMap<String, List<String>> result = new ConcurrentHashMap<String, List<String>>();
        try {
            readFile(file, new Consumer<String>() {
                @Override
                public void consume(String s) {
                    if (s != null && s.trim().length() > 1 && !s.trim().startsWith("#")) {

                        String[] list = s.toLowerCase().trim().split(separator1, 2);
                        if (list.length > 1) {
                            result.putIfAbsent(list[0], new ArrayList<String>());
                            result.get(list[0]).addAll(Arrays.asList(list[1].split(separator2)));
                        }
                    }
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }


    public static List<String> readFileToListOfLines(File file) {
        final List<String> result = new ArrayList<String>();
        try {
            readFile(file, new Consumer<String>() {
                @Override
                public void consume(String s) {
                    result.add(s.trim());
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void readDir(File path, Consumer<File> consumer) throws IOException {
        if (path.exists()) {
            if (path.isDirectory()) {
                File[] files = path.listFiles();
                if (files != null) {
                    for (File file : files) readDir(file, consumer);
                }
            } else consumer.consume(path);

        }
    }

    public static <T> void write(File localFileName, Collection<String> f) {
        String[] fArray = new String[f.size()];
            write(localFileName,f.toArray(fArray));

    }

    public static <T> void write(File localFileName, T... string) {
        Writer out = null;
        try {
            out = utfOut(localFileName);
            for (T t : string) {
                out.write(t.toString());
                out.write("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();

        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }


    @SuppressWarnings({"ResultOfMethodCallIgnored"})
    public static void cleanDirectory(File path) {
        if (path.exists()) {
            File[] files = path.listFiles();
            for (File file : files) {
                if (file.isDirectory()) {
                    cleanDirectory(file);
                } else {
                    file.delete();
                }
            }
        } else {
            path.mkdirs();
        }

    }


}
